package com.cts.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cts.entity.Friend;
import com.cts.mapper.FriendMapper;
import com.cts.model.dto.FriendDTO;
import com.cts.model.vo.FriendVO;
import com.cts.model.vo.PageResultVO;
import com.cts.service.FriendService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author tongshengchen
* @description 针对表【friend】的数据库操作Service实现
* @createDate 2023-02-06 12:25:57
*/
@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend>
    implements FriendService {

    @Autowired
    FriendMapper friendMapper;

    @Override
    public Boolean publishFriend(Integer id, Boolean isPublished) {
        // 判断是否有该条数据
        Friend friend = friendMapper.selectById(id);
        if(Objects.isNull(friend)) {
            return false;
        }

        // 更新友链发布状态
        Boolean flag = friendMapper.updatePublished(id, isPublished);
        return flag;
    }

    @Override
    public List<Friend> getFriendByName(String nickName) {
        List<Friend> list = friendMapper.getFriendByName(nickName);
        return list;
    }


    @Override
    public PageResultVO<Friend> selectFriendList(FriendDTO friendDTO) {
        LambdaQueryWrapper<Friend> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(!StringUtils.isNullOrEmpty(friendDTO.getNickname()),Friend::getNickname, friendDTO.getNickname());
        Page<Friend> page = new Page<>(friendDTO.getPageNo(),friendDTO.getPageSize());
        IPage<Friend> pageResult = friendMapper.selectPage(page, lambdaQueryWrapper);
        return new PageResultVO(pageResult.getTotal(), pageResult.getRecords());
    }

    @Override
    public List<FriendVO> getFriendList() {
        List<FriendVO> friendVOList = friendMapper.getFriendList();
        return friendVOList;
    }

}




